package com.ssm.supplier.controller;



import cn.hutool.core.util.RandomUtil;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ssm.common.utils.PageUtils;
import com.ssm.common.utils.R;
import com.ssm.supplier.entity.SsmGoodsEntity;
import com.ssm.supplier.entity.SsmSudetailsEntity;
import com.ssm.supplier.entity.SsmSuorderEntity;
import com.ssm.supplier.entity.vo.SupplierOrderVo;
import com.ssm.supplier.entity.vo.SuorderDto;
import com.ssm.supplier.service.SsmSudetailsService;
import com.ssm.supplier.service.SsmSuorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 
 *
 * @author ssm
 * @email sunlightcs@gmail.com
 * @date 2023-03-19 17:27:39
 */
@RestController
@RequestMapping("supplier/ssmsuorder")
public class SsmSuorderController {
    @Autowired
    private SsmSuorderService ssmSuorderService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SsmSudetailsService sudetailsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        int currPage = Integer.parseInt(params.get("page").toString());
        int size = Integer.parseInt(params.get("limit").toString());
        String key = params.get("key").toString().trim();
        if (StringUtils.isNotBlank(key)) {
            ArrayList<SuorderDto> orderlist=ssmSuorderService.findPage((currPage-1)*size,size,key);}
//        PageUtils page = ssmSuorderService.queryPage(params);
        ArrayList<SuorderDto> orderlist=ssmSuorderService.findPage((currPage-1)*size,size,key);
        PageUtils page = new PageUtils(orderlist,orderlist.size(),size,currPage);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{suId}")
    public R info(@PathVariable("suId") String suId){
		SsmSuorderEntity ssmSuorder = ssmSuorderService.getById(suId);

        return R.ok().put("ssmSuorder", ssmSuorder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Map<String,Object> Suorder){
        int sumPrice=0;
        SsmSuorderEntity ssmSuorderEntity = new SsmSuorderEntity();
        String refreshToken = Suorder.get("refreshToken").toString();
        String sjId = (String) stringRedisTemplate.opsForHash().get(refreshToken, "sjId");
        ssmSuorderEntity.setSjId(Integer.valueOf(sjId));
        ssmSuorderEntity.setSuId(new Date().getTime()+""+ RandomUtil.randomNumbers(5));
        ArrayList<SsmGoodsEntity> shopcar = (ArrayList<SsmGoodsEntity>) Suorder.get("shopcar");
        for (int i = 0; i < shopcar.size(); i++) {
            String string = JSON.toJSONString(shopcar.get(i));
            SsmGoodsEntity ssmGoods = JSONObject.parseObject(string, SsmGoodsEntity.class);
            sumPrice+= ssmGoods.getGoodsInprice() *ssmGoods.getNumber();
        }
        ssmSuorderEntity.setPrice(sumPrice);
        ssmSuorderEntity.setCreateTime(new Date());
        ssmSuorderEntity.setState("2");
        ssmSuorderService.save(ssmSuorderEntity);

        for (int i = 0; i < shopcar.size(); i++) {
            String string = JSON.toJSONString(shopcar.get(i));
            SsmGoodsEntity ssmGoods = JSONObject.parseObject(string, SsmGoodsEntity.class);
            SsmSudetailsEntity ssmSudetailsEntity = new SsmSudetailsEntity();
            ssmSudetailsEntity.setGoodsId(ssmGoods.getGoodsId());
            ssmSudetailsEntity.setSuId(ssmSuorderEntity.getSuId());
            ssmSudetailsEntity.setGoodsNum(ssmGoods.getNumber());
            ssmSudetailsEntity.setPrice(ssmGoods.getGoodsInprice()*ssmGoods.getNumber());
            sudetailsService.save(ssmSudetailsEntity);
}
        return R.ok().put("orderId",ssmSuorderEntity.getSuId());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody Map<String,Object> ssmSuorder){
        String order = JSON.toJSONString(ssmSuorder.get("order"));
        SsmSuorderEntity suorderEntity = JSONObject.parseObject(order, SsmSuorderEntity.class);
        suorderEntity.setState("1");
        ssmSuorderService.updateById(suorderEntity);

        return R.ok("ok");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] suIds){
		ssmSuorderService.removeByIds(Arrays.asList(suIds));

        return R.ok();
    }

    /**
     * 查询供货订单
     * @param id
     * @return
     */
    @GetMapping("findSupplierOrder/{id}")
    public R findSupplierOrder(@PathVariable("id") Integer id) {
        List<SupplierOrderVo> supplierOrderVoList = ssmSuorderService.findSupplierOrder(id);
        HashMap<String, Object> map=new HashMap<>();
        map.put("supplierOrderList",supplierOrderVoList);

        return R.ok(map);
    }

    /**
     * 更改订单状态
     */
    @PutMapping("/updateOrderState")
    public R updateOrderState(@RequestBody SsmSuorderEntity ssmSuorder){
        ssmSuorderService.updateById(ssmSuorder);
        return R.ok();
    }
    /**
     * 供货端订单查询
     */
    @GetMapping("findOrder/{key}")
    public R findOrder(@PathVariable String key ){
        List<SupplierOrderVo> orderVoArrayList=null;
        if (key!=null){
            orderVoArrayList = ssmSuorderService.findOrderById(key);
        }
        if (orderVoArrayList.size()==0){
            return R.error(404,"没有搜到对应的订单");
        }
        HashMap<String, Object> map=new HashMap<>();
        map.put("orderVoArrayList",orderVoArrayList);
        return R.ok(map);

    }
}
